import {Application, RemoterClass, FrontendSession, ChannelService} from 'pinus';
import { AdWatchDone_req, Kick_revc, PlayerLevel_revc, wxLogin_req } from '../../../package/protocol/ProtoDef';
import { PlayerInfo } from '../../../logic/playerInfo';
import { SessionKey } from '../../../common/enum';

export default function (app: Application) {
    return new AuthRemoter(app);
}

// UserRpc的命名空间自动合并
declare global {
    interface UserRpc {
        connector: {
            // 一次性定义一个类自动合并到UserRpc中
            authRemoter: RemoterClass<FrontendSession, AuthRemoter>;
        };
    }
}


export class AuthRemoter {
    constructor(private app: Application) {
        this.channelService = app.get('channelService');
        
    }
    private channelService: ChannelService;
    
    /**
     * 登录校验
     * @param msg 
     * @returns 
     */
    public async auth(msg: wxLogin_req): Promise<number> {
        //1 判断当前openid在对应区服有没有账户信息,若没有则新建返回playerId
        const playerId: number = 123;
        return playerId;
    }

    /**
     * 将用户加入聊天频道
     *
     * @param {String} uid unique id for user
     * @param {String} sid server id
     * @param {String} name channel name
     * @param {boolean} flag channel parameter
     *
     */
    public async add(uid: string, sid: string, name: string, flag: boolean = true) {
        let channel = this.channelService.getChannel(name, flag);
        if (!!channel) {
            channel.add(uid, sid);
        }
        //后端服务器发小消息
        const param = new AdWatchDone_req();
        param.pos = 123;
        channel.pushMessage('', param); //关播消息

        // //获取当前所有的在线用户
        // const users = this.get(name, flag);
        // console.log(users)

        //单独发送消息
        const singleParam = new PlayerLevel_revc();
        singleParam.level = 68;
        let tuid = uid;
        let tsid = channel.getMember(tuid)['sid'];
        let a = this.app.getServerId()
        this.channelService.pushMessageByUids('', singleParam, [{
            uid: tuid,
            sid: tsid
        }]);
    }

    /**
     * 获取当前频道下所有用户
     *
     * @param {Object} opts parameters for request
     * @param {String} name channel name
     * @param {boolean} flag channel parameter
     * @return {Array} users uids in channel
     *
     */
    private get(name: string, flag: boolean) {
        let users: string[] = [];
        let channelService = this.app.get('channelService')
        let channel = channelService.getChannel(name, flag);
        if (!!channel) {
            users = channel.getMembers();
        }
        return users;
    }

    /**
     * 将用户踢出频道
     *
     * @param {String} uid unique id for user
     * @param {String} sid server id
     * @param {String} name channel name
     *
     */
    public async kick(uid: string, sid: string, name: string) {
        let channel = this.channelService.getChannel(name, false);

        const a = new Kick_revc();
        a.reason = 110;
        channel.pushMessage('', a);

        // leave channel
        if (!!channel) {
            channel.leave(uid, sid);
        }
    }
}